<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Newsclass;
use App\Models\Newsinfo;
use App\Models\Newsinfopic;
use App\Models\Newspopedom;
use Illuminate\Http\Request;

class NewsinfoController extends Controller {

	public function __construct() {
		$this->middleware('role:news');
	}

	/**
	 * 权限检查
	 * @param  string $class_id [description]
	 * @return [type]           [description]
	 */
	private function check_popedom($class_id = '') {
		$result = ['create' => false, 'update' => false, 'confirm' => false, 'delete' => false];
		$token = $this->is_admin();
		if (!$token) {
			return $result;
		}
		$popedom = Newspopedom::get_popedom($class_id, $token['user_id']);
		if ($popedom) {
			$result['create'] = ($popedom & 1) ? true : false;
			$result['update'] = ($popedom & 2) ? true : false;
			$result['confirm'] = ($popedom & 4) ? true : false;
			$result['delete'] = ($popedom & 8) ? true : false;
		}
		if ($this->is_system_admin()) {
			$result = ['create' => true, 'update' => true, 'confirm' => true, 'delete' => true];
		}
		return $result;
	}

	/**
	 * 进入资讯列表页面
	 * @return [type] [description]
	 */
	public function getIndex() {
		$rows = Newsclass::select('id', 'name')->get();
		foreach ($rows as $row) {
			$row->pId = substr($row->id, 0, -4);
		}
		$data['zNodes'] = $rows;
		return view('admin/news/info_list', $data);
	}

	/**
	 * 获取资讯分页列表
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function getPageList(Request $request) {
		$class_id = strval($request->input('class_id'));
		$column = Newsclass::find($class_id);
		$sort_by = empty($column) ? 'DESC' : $column->sort_by;

		$keyword = strval($request->input('keyword'));
		$page = to_limit_lng($request->input('page'), 1);
		$limit = to_limit_lng($request->input('limit'), 1);
		$offset = ($page - 1) * $limit;

		$query = Newsinfo::where('class_id', 'LIKE', $class_id . '%')->where('title', 'LIKE', '%' . $keyword . '%');
		$total_rows = $query->count();
		$rows = $query->orderBy('sortnum', $sort_by)->skip($offset)->take($limit)->get();
		foreach ($rows as $row) {
			$row->pic1 = empty($row->pic1) ? '' : url($row->pic1);
			$row->pic2 = empty($row->pic2) ? '' : url($row->pic2);
		}
		$data['rows'] = $rows;

		$data['total_rows'] = $total_rows;
		$data['page_count'] = ceil($total_rows / $limit);
		$data['page'] = $page;

		return $this->response($data);
	}

	/**
	 * 获取一条资讯信息
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function getInfo(Request $request) {
		$id = intval($request->input('id'));
		$info = Newsinfo::find($id);
		if (empty($info)) {
			return $this->error('无效的信息');
		}

		$column = Newsclass::find($info->class_id);
		$data['column'] = $column;
		$data['info'] = $info;
		return $this->response($data);
	}

	/**
	 * 增加资讯
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postInsert(Request $request) {
		$token = $this->is_admin();
		$class_id = strval($request->input('class_id'));
		$popedom = $this->check_popedom($class_id);
		if ($popedom['create'] == FALSE) {
			return $this->error('无此操作权限');
		}

		$row = new Newsinfo;
		$row->class_id = $class_id;
		$sortnum = intval($request->input('sortnum'));
		if (!$sortnum) {
			$sortnum = Newsinfo::max('sortnum') + 10;
		}
		$row->sortnum = $sortnum;
		$row->title = safe($request->input('title'));
		$row->sub_title = safe($request->input('sub_title'));
		$row->title_color = safe($request->input('title_color'), 10);
		$row->title_bold = safe($request->input('title_bold'), 10);
		$row->first_letter = safe($request->input('first_letter'), 1);

		$row->website = safe($request->input('website'));
		$row->editor = safe($request->input('editor'), 30);
		$row->author = safe($request->input('author'), 30);
		$row->source = safe($request->input('source'), 50);
		$row->publish_at = safe($request->input('publish_at'), 30);
		$row->intro = safe2($request->input('intro'));
		$row->content = safe2($request->input('content'));

		$row->views = intval($request->input('views'));
		$row->is_top = intval($request->input('is_top'));
		$row->is_hot = intval($request->input('is_hot'));
		$row->is_new = intval($request->input('is_new'));
		$row->is_recommend = intval($request->input('is_recommend'));
		$row->is_locked = intval($request->input('is_locked'));
		$row->status = intval($request->input('status'));

		$row->created_user_id = $token['user_id'];
		$row->updated_user_id = $token['user_id'];

		if ($row->save()) {
			return $this->response($row);
		}
		return $this->error('Operation Failed.');
	}

	/**
	 * 修改资讯
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postUpdate(Request $request) {
		$id = intval($request->input('id'));
		$info = Newsinfo::find($id);
		if (empty($info)) {
			return $this->error('无效的信息');
		}

		$token = $this->is_admin();
		$popedom = $this->check_popedom($info->class_id);

		if ($popedom['update']) {
			if ($request->exists('sortnum')) {
				$info->sortnum = intval($request->input('sortnum'));
			}
			if ($request->exists('title')) {
				$info->title = safe($request->input('title'));
			}
			if ($request->exists('sub_title')) {
				$info->sub_title = safe($request->input('sub_title'));
			}
			if ($request->exists('title_color')) {
				$info->title_color = safe($request->input('title_color'), 10);
			}
			if ($request->exists('title_bold')) {
				$info->title_bold = safe($request->input('title_bold'), 10);
			}
			if ($request->exists('first_letter')) {
				$info->first_letter = safe($request->input('first_letter'), 1);
			}
			if ($request->exists('website')) {
				$info->website = safe($request->input('website'));
			}
			if ($request->exists('author')) {
				$info->author = safe($request->input('author'), 30);
			}
			if ($request->exists('editor')) {
				$info->editor = safe($request->input('editor'), 30);
			}
			if ($request->exists('source')) {
				$info->source = safe($request->input('source'), 50);
			}
			if ($request->exists('intro')) {
				$info->intro = safe2($request->input('intro'));
			}
			if ($request->exists('content')) {
				$info->content = safe2($request->input('content'));
			}
			if ($request->exists('is_top')) {
				$info->is_top = intval($request->input('is_top'));
			}
			if ($request->exists('is_new')) {
				$info->is_new = intval($request->input('is_new'));
			}
			if ($request->exists('is_hot')) {
				$info->is_hot = intval($request->input('is_hot'));
			}
			if ($request->exists('is_recommend')) {
				$info->is_recommend = intval($request->input('is_recommend'));
			}
			if ($request->exists('is_locked')) {
				$info->is_locked = intval($request->input('is_locked'));
			}
			if ($request->exists('views')) {
				$info->views = intval($request->input('views'));
			}
			if ($request->exists('publish_at')) {
				$info->publish_at = safe($request->input('publish_at'), 30);
			}
			$info->updated_user_id = $token['user_id'];
		}

		if ($popedom['confirm']) {
			if ($request->exists('status')) {
				$info->status = intval($request->input('status'));
			}
		}

		if ($info->save()) {
			return $this->response($info);
		}
		return $this->error('Operation Failed.');
	}

	/**
	 * 上传资讯小图
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postUploadPic1(Request $request) {
		$id = intval($request->input('id'));
		$file = $_FILES['pic'];

		$errorCode = $file['error'];
		if ($errorCode !== UPLOAD_ERR_OK) {
			return $this->error($errorCode);
		}

		$type = exif_imagetype($file['tmp_name']);
		if ($type === false) {
			return $this->error('无效的图片文件');
		}
		if (!in_array($type, [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG])) {
			return $this->error('无效的图片格式');
		}

		$target_path = 'upload/' . date('Y/m/d/');
		$targetPath = public_path() . DIRECTORY_SEPARATOR . $target_path;
		if (!is_dir($targetPath)) {
			if (!mkdir($targetPath, 0777, 1)) {
				return $this->error('无法建立上传目录');
			}
		}

		$new_filename = uniqid(mt_rand()) . image_type_to_extension($type);
		$result = move_uploaded_file($file['tmp_name'], $targetPath . $new_filename);
		if ($result === false) {
			return $tis->error('保存文件失败');
		}
		$image = $target_path . $new_filename;

		$row = Newsinfo::find($id);
		!!$row->pic1 AND unlink($row->pic1);
		$row->pic1 = $image;
		if ($row->save()) {
			return $this->response(url($image));
		}
		return $this->error('Operation Failed.');
	}

	/**
	 * 删除资讯小图
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postRemovePic1(Request $request) {
		$id = intval($request->input('id'));
		$row = Newsinfo::find($id);
		file_exists($row->pic1) AND unlink($row->pic1);
		$row->pic1 = '';
		$row->save();
		return $this->response('ok');
	}

	/**
	 * 上传资讯大图
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postUploadPic2(Request $request) {
		$id = intval($request->input('id'));
		$file = $_FILES['pic'];

		$errorCode = $file['error'];
		if ($errorCode !== UPLOAD_ERR_OK) {
			return $this->error($errorCode);
		}

		$type = exif_imagetype($file['tmp_name']);
		if ($type === false) {
			return $this->error('无效的图片文件');
		}
		if (!in_array($type, [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG])) {
			return $this->error('无效的图片格式');
		}

		$target_path = 'upload/' . date('Y/m/d/');
		$targetPath = public_path() . DIRECTORY_SEPARATOR . $target_path;
		if (!is_dir($targetPath)) {
			if (!mkdir($targetPath, 0777, 1)) {
				return $this->error('无法建立上传目录');
			}
		}

		$new_filename = uniqid(mt_rand()) . image_type_to_extension($type);
		$result = move_uploaded_file($file['tmp_name'], $targetPath . $new_filename);
		if ($result === false) {
			return $tis->error('保存文件失败');
		}
		$image = $target_path . $new_filename;

		$row = Newsinfo::find($id);
		!!$row->pic2 AND unlink($row->pic2);
		$row->pic2 = $image;
		if ($row->save()) {
			return $this->response(url($image));
		}
		return $this->error('Operation Failed.');
	}

	/**
	 * 删除资讯大图
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postRemovePic2(Request $request) {
		$id = intval($request->input('id'));
		$row = Newsinfo::find($id);
		file_exists($row->pic2) AND unlink($row->pic2);
		$row->pic2 = '';
		$row->save();
		return $this->response('ok');
	}

	/**
	 * 信息移动
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postMove(Request $request) {
		$target_class_id = strval($request->input('class_id'));
		$popedom = $this->check_popedom($target_class_id);
		if ($popedom['create'] == false) {
			return $this->error('目录栏目没有写入权限');
		}

		$ids = strval($request->input('ids'));
		if (empty($ids)) {
			return $this->error('无效的操作');
		}
		$ids = explode('|', $ids);
		$rows = Newsinfo::whereIn('id', $ids)->get();
		$result = [];
		foreach ($rows as $row) {
			if ($row->is_locked) {
				continue;
			}
			if ($row->class_id == $target_class_id) {
				continue;
			}
			$popedom = $this->check_popedom($row->class_id);
			if ($popedom['delete'] == false) {
				return $this->error('无此操作权限');
			}
			$row->class_id = $target_class_id;
			if ($row->save()) {
				$result[] = $row->id;
			}
		}
		return $this->response($result);
	}

	/**
	 * 删除资讯
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function postDelete(Request $request) {
		$ids = strval($request->input('ids'));
		if (empty($ids)) {
			return $this->error('无效的操作');
		}
		$ids = explode('|', $ids);
		$rows = Newsinfo::whereIn('id', $ids)->get();
		$result = [];
		foreach ($rows as $row) {
			$id = $row->id;
			if ($row->is_locked) {
				continue;
			}
			file_exists($row->pic1) && unlink($row->pic1);
			file_exists($row->pic2) && unlink($row->pic2);
			file_exists($row->file1) && unlink($row->file1);
			if ($row->delete()) {
				Newsinfopic::deleteByInfoId($id);
				$result[] = $id;
			}
		}
		return $this->response($result);
	}

}
